package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * description: 
 * @author zhufeng
 * @date 2024/3/29 0029 9:05
 * @version V1.0
 */
public class 元素和最小的山形三元组I_2908 {
    public int minimumSum(int[] nums) {
        int min = Integer.MAX_VALUE;
        int minLeft = nums[0];
        int minRight;
        for (int j = 1; j <= nums.length - 2; j++) {
            minLeft = Math.min(minLeft, nums[j-1]);
            minRight = getMinRight(j+1, nums);
            if (minLeft < nums[j] && minRight < nums[j]) {
                min = Math.min(min, minLeft+ nums[j]+minRight);
            }
        }
        return min == Integer.MAX_VALUE ? -1 : min;
    }

    private int getMinRight(int index, int[] nums) {
        int min = nums[index];
        for (int i = index+1; i < nums.length; i++) {
            min = Math.min(min, nums[i]);
        }
        return min;
    }

    @Test
    public void testMinimumSum () {
        int[] nums = {8,6,1,5,3};
        System.out.println(minimumSum(nums));
    }
}
